package com.easonji.common.factory;

import com.baomidou.mybatisplus.plugins.Page;
import com.easonji.common.constant.enums.OrderEnum;
import com.easonji.common.util.HttpUtils;
import com.easonji.common.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: PageFactory
 * @Description: BootStrap Table默认的分页参数创建
 * @Author: LiuJun
 * @date: 2018/07/09 11:43
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
public class PageFactory<T> {

    public Page<T> defaultPage() {
        HttpServletRequest request = HttpUtils.getRequest();
        // 每页多少条数据
        int limit = Integer.valueOf(request.getParameter("limit"));
        // 每页的偏移量(本页当前有多少条)
        int offset = Integer.valueOf(request.getParameter("offset"));
        // 排序字段名称
        String sort = request.getParameter("sort");
        // asc或desc(升序或降序)
        String order = request.getParameter("order");
        if (StringUtils.isEmpty(sort)) {
            Page<T> page = new Page<>((offset / limit + 1), limit);
            page.setOpenSort(false);
            return page;
        } else {
            Page<T> page = new Page<>((offset / limit + 1), limit, sort);
            if (OrderEnum.ASC.getOrder().equals(order)) {
                page.setAsc(true);
            } else {
                page.setAsc(false);
            }
            return page;
        }
    }
}
